import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { MaxStepMoneyhStrategy } from "./NormalStepMoneyStrategy";
import { WithRecmMoneyStrategy } from "./WithRecmMoneyStrategy";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";


export class WisdomMoneyStrategyV98 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV91Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    protected _autoSwitch: boolean = false;
    public get autoSwitch(): boolean { return this._autoSwitch; }
    public set autoSwitch(value: boolean) { this._autoSwitch = value; }
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string, autoSwitch?: boolean) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this._autoSwitch = autoSwitch ? true : false;
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {
        let plan1 = new ExcutePlan();
        plan1.recmStrategy = [new FixedStrategy({ reverse: false })]
        plan1.moneyStrategy = [new MaxStepMoneyhStrategy("方案一", {
            baseMoney: 10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true)];
        plan1.toResult(new RoundInfo());

        let plan2 = new ExcutePlan();
        plan2.recmStrategy = [new FixedStrategy({ reverse: true })];
        plan2.moneyStrategy = [new MaxStepMoneyhStrategy("方案二", {
            baseMoney: 10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true)];
        plan2.toResult(new RoundInfo());

        var planG1 = new ExcutePlan();
        planG1.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG1.moneyStrategy = [new WithRecmMoneyStrategy("跟投1", 1, false, new MaxStepMoneyhStrategy("", {
            baseMoney: 10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG1.toResult(new RoundInfo());
        var planG1F = new ExcutePlan();
        planG1F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG1F.moneyStrategy = [new WithRecmMoneyStrategy("跟投1反", 1, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG1F.toResult(new RoundInfo());

        var planG2 = new ExcutePlan();
        planG2.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG2.moneyStrategy = [new WithRecmMoneyStrategy("跟投2", 2, false, new MaxStepMoneyhStrategy("", {
            baseMoney: 10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG2.toResult(new RoundInfo());
        var planG2F = new ExcutePlan();
        planG2F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG2F.moneyStrategy = [new WithRecmMoneyStrategy("跟投2反", 2, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG2F.toResult(new RoundInfo());

        var planG3 = new ExcutePlan();
        planG3.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG3.moneyStrategy = [new WithRecmMoneyStrategy("跟投3", 3, false, new MaxStepMoneyhStrategy("", {
            baseMoney: 10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG3.toResult(new RoundInfo());
        var planG3F = new ExcutePlan();
        planG3F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG3F.moneyStrategy = [new WithRecmMoneyStrategy("跟投3反", 3, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG3F.toResult(new RoundInfo());


        var planG4 = new ExcutePlan();
        planG4.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG4.moneyStrategy = [new WithRecmMoneyStrategy("跟投4", 4, false, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG4.toResult(new RoundInfo());
        var planG4F = new ExcutePlan();
        planG4F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG4F.moneyStrategy = [new WithRecmMoneyStrategy("跟投4反", 4, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG4F.toResult(new RoundInfo());

        var planG5 = new ExcutePlan();
        planG5.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG5.moneyStrategy = [new WithRecmMoneyStrategy("跟投5", 5, false, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG5.toResult(new RoundInfo());
        var planG5F = new ExcutePlan();
        planG5F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG5F.moneyStrategy = [new WithRecmMoneyStrategy("跟投5反", 5, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG5F.toResult(new RoundInfo());

        var planG6 = new ExcutePlan();
        planG6.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG6.moneyStrategy = [new WithRecmMoneyStrategy("跟投6", 6, false, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG6.toResult(new RoundInfo());
        var planG6F = new ExcutePlan();
        planG6F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG6F.moneyStrategy = [new WithRecmMoneyStrategy("跟投6反", 6, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG6F.toResult(new RoundInfo());

        var planG7 = new ExcutePlan();
        planG7.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG7.moneyStrategy = [new WithRecmMoneyStrategy("跟投7", 7, false, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG7.toResult(new RoundInfo());
        var planG7F = new ExcutePlan();
        planG7F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG7F.moneyStrategy = [new WithRecmMoneyStrategy("跟投7反", 7, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG7F.toResult(new RoundInfo());

        var planG8 = new ExcutePlan();
        planG8.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG8.moneyStrategy = [new WithRecmMoneyStrategy("跟投8", 8, false, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG8.toResult(new RoundInfo());
        var planG8F = new ExcutePlan();
        planG8F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG8F.moneyStrategy = [new WithRecmMoneyStrategy("跟投8反", 8, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG8F.toResult(new RoundInfo());

        var planG9 = new ExcutePlan();
        planG9.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG9.moneyStrategy = [new WithRecmMoneyStrategy("跟投9", 9, false, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG9.toResult(new RoundInfo());
        var planG9F = new ExcutePlan();
        planG9F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG9F.moneyStrategy = [new WithRecmMoneyStrategy("跟投9反", 9, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG9F.toResult(new RoundInfo());

        var planG10 = new ExcutePlan();
        planG10.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG10.moneyStrategy = [new WithRecmMoneyStrategy("跟投10", 10, false, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG10.toResult(new RoundInfo());
        var planG10F = new ExcutePlan();
        planG10F.recmStrategy = [new FixedStrategy({ reverse: false })];
        planG10F.moneyStrategy = [new WithRecmMoneyStrategy("跟投10反", 10, true, new MaxStepMoneyhStrategy("", {
            baseMoney:10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true))];
        planG3F.toResult(new RoundInfo());


        this.subPlan = [plan1, plan2,
            planG1, //planG1F,
            planG2, //planG2F,
            planG3, //planG3F,
            planG4, //planG4F,
            planG5, //planG5F,
            planG6, //planG6F,
            //planG7, //planG7F,
            //planG8, //planG8F,
            //planG9, //planG9F,
            //planG10, //planG10F,
        ];

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;

        this.subPlan.forEach((pl, index) => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
            let userRecmV = pl.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;

            sumWin += (pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney);

            let [rv, rm] = pl.toMoney(newRoundInfo, pl.recmInfo, userRecm, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }

            if (this._autoSwitch) {
                if (pl.recmInfo.summaryInfo.winMoney > pl.recmInfo.summaryInfo.failMoney) {
                    if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                        zm += pl.recmInfo.recmMoney;

                    }
                    else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                        xm += pl.recmInfo.recmMoney;
                    }
                }

            }
            else {
                if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                    zm += pl.recmInfo.recmMoney;

                }
                else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                    xm += pl.recmInfo.recmMoney;
                }

            }

        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        this._subPlanSum = sumWin;
        
        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV98;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}